package common.mq.receiver.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import common.mq.basic.service.MessageService;
import common.mq.basic.service.QueueService;
import common.mq.master.service.IMasterService;
import common.mq.po.Message;
import common.mq.po.Queue;
import common.mq.receiver.po.SendResult;
import common.mq.receiver.service.IReceiveService;
import common.mq.receiver.service.IRemoveDuplicateService;
import common.mq.receiver.thrift.TMessageInfo;
import common.mq.register.po.Consumer;
import common.mq.register.po.MessageBase;
import common.mq.register.service.IRegisterService;

/**
 * 接收器和调度器实现类
 * 
 * @author Administrator
 * 
 */
public class ReceiveServiceImpl implements IReceiveService{

	final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(ReceiveServiceImpl.class);

	@Autowired
	private IRemoveDuplicateService removeDuplicateService;
	@Autowired
	private IMasterService masterService;
	@Autowired
	private IRegisterService registerService;
	@Autowired
	private MessageService messageService;
	@Autowired
	private QueueService queueService;
	
	@Override
	public void setRemoveDuplicateService(
			IRemoveDuplicateService removeDuplicateService) {
		this.removeDuplicateService = removeDuplicateService;
	}

	@Override
	public void setMasterService(IMasterService masterService) {
		this.masterService = masterService;
	}

	@Override
	public void setRegisterService(IRegisterService registerService) {
		this.registerService = registerService;
	}

	@Override
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	@Override
	public void setQueueService(QueueService queueService) {
		this.queueService = queueService;
	}

	@Override
	public SendResult receiveMessage(TMessageInfo msgInfo) {
		
		if (msgInfo == null){
			return SendResult.NULL;
		}

		SendResult result = checkMessage(msgInfo);
		if (!result.equal(SendResult.SUCCESS)) {
			return result;
		}

		if (removeDuplicateService.handle(msgInfo)) {// 排重
			return SendResult.DUPLICATE;
		}

		Message message = null;
		try {
			message = messageService.genMessage(msgInfo.getCategory(),(int) System.currentTimeMillis() / 1000, msgInfo.getContent());
		} catch (Exception e) {
			return SendResult.VALID;
		}
		if (message == null) { // 消息是否为空
			return SendResult.NULL;
		} else if (!messageService.checkMessageFormat(message)) {
			//仅检查MAP属性中简单类型，若是LIST类型仅检查到是否是符合LIST结构的JSON格式
			return SendResult.VALID;
		}

		if (sendMessage(message)) {
			return SendResult.SUCCESS;
		}

		return SendResult.FAIL;
	}

	private SendResult checkMessage(TMessageInfo msgInfo) {
		SendResult result = SendResult.VALID;
		if (!registerService.hasRegister(msgInfo.getCategory())) // 消息元不包含此类别
			result.setMsg("不允许发送此类消息！");
		else if (!registerService.isUidExist(msgInfo.getCaller())) { // 是否存在该用户注册信息
			result.setMsg("此用户不允许发送消息！");
//		} else if (msgInfo.getContent().trim().isEmpty()) { // 消息内容是否为空
//			result.setMsg("消息内容不能为空！");
		} else {
			result = SendResult.SUCCESS;
		}
		return result;
	}

	private boolean sendMessage(Message message) {
		String category = message.getCategory();
		List<Consumer> consumers = registerService.getConsumers(category);
		boolean result = true;
		for (Consumer consumer : consumers) {
			Queue queue = masterService.getQueue(consumer.getId());
			if (queue == null) {
				result = false;
				break;
			}
			try {
				queueService.receive(message, queue);
			} catch (Exception e) {
				result = false;
				break;
			}
		}
		return result;
	}
}
